package com.ruoyi.gcgl.service.impl;

import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.activiti.engine.IdentityService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.gcgl.mapper.GcJhksqdMapper;
import com.ruoyi.gcgl.domain.GcFyhx;
import com.ruoyi.gcgl.domain.GcJhksqd;
import com.ruoyi.gcgl.service.IGcJhksqdService;
import com.ruoyi.common.core.text.Convert;

/**
 * 借还款申请单Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-12-17
 */
@Service
public class GcJhksqdServiceImpl implements IGcJhksqdService 
{
    @Autowired
    private GcJhksqdMapper gcJhksqdMapper;
    @Resource
    private RuntimeService runtimeService;
    @Resource
    private TaskService taskService;
    @Resource
    IdentityService identityService;

    /**
     * 查询借还款申请单
     * 
     * @param id 借还款申请单主键
     * @return 借还款申请单
     */
    @Override
    public GcJhksqd selectGcJhksqdById(Long id)
    {
        return gcJhksqdMapper.selectGcJhksqdById(id);
    }

    /**
     * 查询借还款申请单列表
     * 
     * @param gcJhksqd 借还款申请单
     * @return 借还款申请单
     */
    @Override
    public List<GcJhksqd> selectGcJhksqdList(GcJhksqd gcJhksqd)
    {
        return gcJhksqdMapper.selectGcJhksqdList(gcJhksqd);
    }

    /**
     * 新增借还款申请单
     * 
     * @param gcJhksqd 借还款申请单
     * @return 结果
     */
    @Override
    public int insertGcJhksqd(GcJhksqd gcJhksqd)
    {
    	int rows = gcJhksqdMapper.insertGcJhksqd(gcJhksqd);
    	// 发起合同审核流程
        identityService.setAuthenticatedUserId(gcJhksqd.getOwner());
        HashMap<String, Object> variables = new HashMap<>();
        variables.put("applyuserid", gcJhksqd.getOwner());
        variables.put("level1", gcJhksqd.getApprovename());
        ProcessInstance pi = runtimeService.startProcessInstanceByKey("sjjiehuankuan", String.valueOf(gcJhksqd.getId()), variables);
    	
        GcJhksqd obj = new GcJhksqd();
    	obj.setId(gcJhksqd.getId());
    	obj.setProcessInstanceId(pi.getProcessInstanceId());
    	gcJhksqdMapper.updateGcJhksqd(obj);
    	
        return rows;
    }

    /**
     * 修改借还款申请单
     * 
     * @param gcJhksqd 借还款申请单
     * @return 结果
     */
    @Override
    public int updateGcJhksqd(GcJhksqd gcJhksqd)
    {
    	// 发起合同审核流程
//        identityService.setAuthenticatedUserId(gcJhksqd.getOwner());
//        HashMap<String, Object> variables = new HashMap<>();
//        variables.put("applyuserid", gcJhksqd.getOwner());
//        variables.put("level1", gcJhksqd.getApprovename());
//        ProcessInstance pi = runtimeService.startProcessInstanceByKey("sjjiehuankuan", String.valueOf(gcJhksqd.getId()), variables);
//        gcJhksqd.setProcessInstanceId(pi.getProcessInstanceId());
        return gcJhksqdMapper.updateGcJhksqd(gcJhksqd);
    }

    /**
     * 批量删除借还款申请单
     * 
     * @param ids 需要删除的借还款申请单主键
     * @return 结果
     */
    @Override
    public int deleteGcJhksqdByIds(String ids)
    {
        return gcJhksqdMapper.deleteGcJhksqdByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除借还款申请单信息
     * 
     * @param id 借还款申请单主键
     * @return 结果
     */
    @Override
    public int deleteGcJhksqdById(Long id)
    {
        return gcJhksqdMapper.deleteGcJhksqdById(id);
    }

	@Override
	public int updateGcJhksqd4Check(GcJhksqd gcJhksqd) {
		return gcJhksqdMapper.updateGcJhksqd(gcJhksqd);
	}
}
